var page = 1;
var maxpage;
$('#more').hide();

$(function () {
    $('.weui-tab').tab({
        defaultIndex: 0,
        activeClass: 'weui-bar__item_on',
        onToggle: function (index) {
            if (index === 0) {
            } else if (index === 1) {
                location.href = "/page/watch";
            } else if (index === 2) {
                location.href = "/page/my";
            }
        }
    });

    var l = sessionStorage.getItem('index_list');
    if (null !== l && '' !== l) {
        $("#rank-list").html(l);
        $(window).scrollTop(sessionStorage.getItem('index_scroll'));
        page = sessionStorage.getItem('index_page');
        sessionStorage.removeItem('index_list');
        sessionStorage.removeItem('index_page');
        sessionStorage.removeItem('index_scroll');

        $('.weui-cell_swiped').swipeout('close');
    } else {
        ajaxpage(1);
    }
});

function ajaxpage(page) {
    var name = $("#searchInput").val();
    $.ajax({
        type: "GET",
        url: '/product/get-product-info-by-paging?pageNum=' + page + '&name=' + name,
        beforeSend: function () {
            $("#more").show();
        },
        success: function (result) {
            document.getElementById("more").style.display = "none";
            if (result.code === "0") {
                if (result.data.totalCount === 0 || result.data.totalCount < 20) {
                    document.getElementById("none").style.display = "block";
                }
                $("#rank-list").append(tpl(document.getElementById('tpl').innerHTML, result.data));
                $('.weui-cell_swiped').swipeout();

                $('.delete-swipeout').click(function () {
                    var dataId = $(this).parents('.weui-cell').data('id');
                    var strs = new Array(); //定义一数组
                    strs = dataId.split("|");
                    var id = strs[0];
                    var mark = strs[1];
                    var jsonData = {"skuid": id, "watchStatus": mark};
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json",
                        url: "/product/watch-or-unwatch-product",
                        data: JSON.stringify(jsonData),
                        success: function (result) {
                            if (result.code === "0") {
                                $.toast.prototype.defaults.duration = 1000;//1秒
                                $.toast("操作成功", 'text');
                            } else if (result.code === "101001") {
                                // window.location.href = "/page/login";
                                $.toast.prototype.defaults.duration = 1000;//1秒
                                $.toast(result.message, 'text');
                            } else {
                                $.toast.prototype.defaults.duration = 1000;//1秒
                                $.toast(result.message, 'text');
                            }
                        },
                        error: function () {
                            $.toast.prototype.defaults.duration = 1000;//1秒
                            $.toast("无网络", 'text');
                        }
                    });
                    $(this).parents('.weui-cell').remove();
                });

                $('.close-swipeout').click(function () {
                    $(this).parents('.weui-cell').swipeout('close')
                });
                maxpage = result.data.totalPage;
                sessionStorage['maxpage'] = maxpage;
            } else if (result.code === "101001") {
                // location.href = "/page/login";
                $.toast.prototype.defaults.duration = 1000;//1秒
                $.toast(result.message, 'text');
            } else {
                $.toast.prototype.defaults.duration = 1000;//1秒
                $.toast(result.message, 'text');
            }
        },
        error: function () {
            document.getElementById("more").style.display = "none";
            $.toast.prototype.defaults.duration = 1000;//1秒
            $.toast("无网络", 'text');
        },
        timeout: 15000
    });
}

$(window).scroll(
    function () {
        var scrollTop = $(this).scrollTop();
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if (scrollTop + windowHeight === scrollHeight) {
            maxpage = sessionStorage['maxpage'];
            if (page < maxpage) {
                page++;
                ajaxpage(page);
            } else {
                document.getElementById("none").style.display = "block";
                document.getElementById("more").style.display = "none";
                return false;
            }
        }
    }
);

const goto = (id) => {
    sessionStorage.setItem('index_list', $("#rank-list").html());//存储列表数据
    sessionStorage.setItem('index_page', page);//存储页码
    sessionStorage.setItem('index_scroll', $(window).scrollTop());//存储滚动条位置
    location.href = "/page/detail?skuid=" + id;
};
$(document).on('tap click', "#rank-list .weui-cell .weui-cell", function () {
    var dataId = $(this).parents('.weui-cell').data('id');
    var strs = new Array(); //定义一数组
    strs = dataId.split("|");
    var skuid = strs[0];
    goto(skuid);
});

<!-- 搜索功能 -->
$(document).on("click", "#btnSearch", function () {
    $('#rank-list').empty();
    document.getElementById("none").style.display = "none";
    page = 1;
    ajaxpage(1);
});